package com.yj.suncity.collect;

import java.util.List;

import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.activity.shouye.CityHelpCommentActivity;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.collect.bean.CollectHelpBean;
import com.yj.suncity.collect.bean.CollectListBean;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.CircleImageView;
import com.yj.suncity.utis.DialogUtils;
import com.yj.suncity.utis.ToastUtil;
import com.yj.suncity.utis.Utils;
import com.yj.suncity.utis.DialogUtils.ReshActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CollectHelpListActivity extends Activity {
	private Context context;
	private String userId;
	private ListView mList_collect_view;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_collect_list);
		context = this;
		userId = getIntent().getStringExtra("userid");
		findView();
		initData();
	}

	private void findView() {
		((TextView) findViewById(R.id.txt_title_name)).setText("收藏帮帮");
		TextView emptyView = (TextView) findViewById(R.id.txt_empty);
		mList_collect_view = (ListView) findViewById(R.id.list_collect_view);
		mList_collect_view.setEmptyView(emptyView);
	}

	private void initData() {
		String path = URLCon.收藏 + "?action=getcityhelpcollectionlist&userid="
				+ userId;
		Log.d("", path);

		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
				false, null, true, "正在加载......", new ResultCallBack() {

					@Override
					public void JSONResult(JSONObject jsonObject) {
						CollectListBean listBean = new CollectListBean(
								jsonObject);
						mList_collect_view.setAdapter(new CityHelpAdapter(
								listBean.getHelpData()));
					}
				});
		asyncTaskUtil.execute(path);
	}

	class CityHelpAdapter extends BaseAdapter {
		private List<CollectHelpBean> helpBeans;

		public CityHelpAdapter(List<CollectHelpBean> helpBeans) {
			super();
			this.helpBeans = helpBeans;
			imageLoader = ImageLoader.getInstance();
		}

		@Override
		public int getCount() {
			return helpBeans.size();
		}

		@Override
		public Object getItem(int position) {
			return helpBeans.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.adapter_city_help_list, null);

				holder.mImg_head = (CircleImageView) convertView
						.findViewById(R.id.img_head);
				holder.mTxt_release_username = (TextView) convertView
						.findViewById(R.id.txt_release_username);
				holder.mTxt_release_time = (TextView) convertView
						.findViewById(R.id.txt_release_time);
				holder.mTxt_gave_num = (TextView) convertView
						.findViewById(R.id.txt_give_num);
				holder.mTxt_help_content = (TextView) convertView
						.findViewById(R.id.txt_help_content);
				holder.mTxt_see_count = (TextView) convertView
						.findViewById(R.id.txt_see_count);
				holder.mTxt_comment_count = (TextView) convertView
						.findViewById(R.id.txt_comment_count);
				holder.mTxt_hold_count = (TextView) convertView
						.findViewById(R.id.txt_hold_count);
				holder.layout = (LinearLayout) convertView
						.findViewById(R.id.lay_linear_item);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final CollectHelpBean helpBean = helpBeans.get(position);
			holder.mTxt_release_username
					.setText(helpBean.getRelease_username());
			holder.mTxt_release_time.setText(""
					+ Utils.DateFormatDate(helpBean.getRelease_datetime()));
			holder.mTxt_gave_num.setText(helpBean.getGive_num());
			holder.mTxt_help_content.setText(helpBean.getHelp_title());
			holder.mTxt_see_count.setText(helpBean.getSee_count());
			holder.mTxt_comment_count.setText(helpBean.getComment_count());
			holder.mTxt_hold_count.setText(helpBean.getHold_count());
			if (!helpBean.getHead_photo().equals("")) {
				imageLoader.displayImage(
						URLCon.HOST + helpBean.getHead_photo(),
						holder.mImg_head, options);
			} else {
				holder.mImg_head
						.setImageResource(R.drawable.android_layout_detail);
			}
			holder.layout.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,
							CityHelpCommentActivity.class);
					intent.putExtra("helpid", helpBean.getId());
					intent.putExtra("datetime", helpBean.getRelease_datetime());
					intent.putExtra("username", helpBean.getRelease_username());
					intent.putExtra("give_num", helpBean.getGive_num());
					intent.putExtra("help_content", helpBean.getHelp_content());
					intent.putExtra("head_photo", helpBean.getHead_photo());
					startActivityForResult(intent, 1);
				}
			});
			holder.layout.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {
					DialogUtils dialogUtils = DialogUtils.getInstance();
					dialogUtils.backDialog(context, "确定删除吗？", true,
							new ReshActivity() {
								@Override
								public void reshActivity() {
									deleteCollect(helpBean.getId());
								}
							});
					return false;
				}
			});
			return convertView;
		}
	}

	class ViewHolder {
		CircleImageView mImg_head;
		TextView mTxt_release_username;
		TextView mTxt_release_time;
		TextView mTxt_gave_num;
		TextView mTxt_help_content;
		TextView mTxt_see_count;
		TextView mTxt_comment_count;
		TextView mTxt_hold_count;
		LinearLayout layout;
	}

	private void deleteCollect(String collectid) {
		String path = URLCon.收藏 + "?action=delcollection&userid=" + userId
				+ "&id=" + collectid;
		Log.d("", "删除：" + path);
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
				false, null, true, "正在删除......", new ResultCallBack() {

					@Override
					public void JSONResult(JSONObject jsonObject) {
						if (jsonObject.optString("success").equals("1")) {
							initData();
						} else {
							ToastUtil.ShowToast(context,
									"" + jsonObject.optString("msg"));
						}
					}
				});
		asyncTaskUtil.execute(path);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1 && resultCode == 1) {
           initData();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
